package com.zhuhjay.common.base.model.vo;

import com.zhuhjay.common.base.enums.HttpCode;
import lombok.Data;

import java.io.Serializable;

/**
 * @author ZhuHJay
 * @date 2023/2/25 18:59
 */
@Data
public class HttpResult<T> implements Serializable {

    private int code;
    private String message;
    private T data;

    private HttpResult(int code, String message) {
        this.code = code;
        this.message = message;
    }

    private HttpResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static HttpResult<?> ok() {
        return new HttpResult<>(HttpCode.SUCCESS.getCode(), HttpCode.SUCCESS.getDesc());
    }

    public static HttpResult<?> ok(HttpCode code) {
        return new HttpResult<>(code.getCode(), code.getDesc());
    }

    public static HttpResult<?> ok(String message) {
        return new HttpResult<>(HttpCode.SUCCESS.getCode(), message);
    }

    public static <T> HttpResult<T> ok(T data) {
        return new HttpResult<>(HttpCode.SUCCESS.getCode(), HttpCode.SUCCESS.getDesc(), data);
    }

    public static HttpResult<?> fail() {
        return new HttpResult<>(HttpCode.USER_ERROR.getCode(), HttpCode.USER_ERROR.getDesc());
    }

    public static HttpResult<?> fail(HttpCode code) {
        return new HttpResult<>(code.getCode(), code.getDesc());
    }

    public static HttpResult<?> fail(String message) {
        return new HttpResult<>(HttpCode.USER_ERROR.getCode(), message);
    }

}
